<template>
  <div class="
    white-module">
    <div class="scholl-tabs">
      <!--<el-button class="tabs-btn" type="primary">添加学校</el-button>-->
      <el-tabs v-model="activeName">
        <el-tab-pane label="学校列表" name="first"></el-tab-pane>
      </el-tabs>
    </div>
    <el-row class="search-module" >
      <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()" style="width: 100%">
        <el-col class="search-left" :span="18">
          学校名称：<el-input  v-model="dataForm.like_schoolName" placeholder="请输入学校名称" />
          <span class="interval"></span>
        </el-col>
        <el-col class="right-btn" :span="6">
          <el-button type="primary" @click="getDataList()" >查询</el-button>
          <el-button type="default" @click="clearForm()">重置</el-button>
        </el-col>
      </el-form>
    </el-row>
    <div class="global-table">
      <el-table ref="multipleTable"
          :data="dataList"
          :header-cell-style="{
          background: '#F5F7FA',
          color: '#333',
        }"
          style="width: 100%"
      >
        <el-table-column prop="schoolName" label="学校名称" />
        <el-table-column prop="teacherNum" label="老师数" />
        <el-table-column prop="studentNum" label="学生数" />
        <el-table-column label="操作" width="250">
          <template #default="scope">
            <!--<el-button type="text" size="small" @click="showDialog(scope.row)">修改学校名称</el-button>-->
            <el-button type="text" size="small" @click="showResetPassDialog(scope.row)"> 重置密码 </el-button>
            <el-button type="text" size="small" @click="showFenpeiDialog(scope.row)"> 分配课程 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-row class="global-pagination" >
      <el-col :span="8" class="total"> 共搜索 {{total}} 条数据 </el-col>
      <el-col :span="16">
        <el-pagination
            background
            layout="  prev, pager, next, jumper"
            :current-page="page"  :page-sizes="[10, 20, 50, 100]" :page-size="limit"
            :total="total"
            :small="false"
            :disabled="false"
            @size-change="query"
            @current-change="pageCurrentChangeHandle"
        />
      </el-col>
    </el-row>

    <!--修改学校名称-->
    <el-dialog title="修改" v-model="showDialogFlag">
      <el-form :model="updateForm">
        <el-form-item label="学校名称" :label-width="formLabelWidth">
          <el-input v-model="updateForm.schoolName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialogFlag = false">取 消</el-button>
        <el-button type="primary" @click="updateSchool">确 定</el-button>
      </div>
    </el-dialog>

    <!--分配课程-->
    <el-dialog title="修改" v-model="showFenpeiDialogFlag">
      <el-select v-model="selCourseId" placeholder="请选择课程" style="width: 250px">
        <el-option
            v-for="item in courseList"
            :key="item.title"
            :label="item.title"
            :value="item.id"
            :disabled="item.hasFenpei"
        />
      </el-select>
      <el-button type="primary" @click="addOrDelCourse('add', null)">添加</el-button>
      <el-table ref="multipleTable"  :data="courseSelectedList" style="width: 100%" @select="selectChecked"  :row-key="getRowKeys">
        <el-table-column type="selection" :reserve-selection="true" ></el-table-column>
        <el-table-column prop="id" label="编号" ></el-table-column>
        <el-table-column prop="title" label="课程名称" ></el-table-column>
        <el-table-column label="操作" width="250">
          <template #default="scope">
            <el-button type="text" size="small" @click="addOrDelCourse('del', scope.row.id)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
      </div>
    </el-dialog>

    <!--重置密码-->
    <el-dialog title="重置密码" v-model="showResetPass">
      <el-form :model="resetPassForm">
        <el-form-item label="输入新密码" :label-width="formLabelWidth">
          <el-input v-model="resetPassForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="再次输入密码" :label-width="formLabelWidth">
          <el-input v-model="resetPassForm.password2" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showResetPass = false">取 消</el-button>
        <el-button type="primary" @click="resetPass">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>


<script lang="ts" >
import { defineComponent, reactive, toRefs,ref, onMounted } from "vue";
import {ElMessage, ElTable} from 'element-plus'
import listService from "@/service/listService";
import baseService from "@/service/baseService";

export default defineComponent({
  setup() {

    const state = reactive({
      getDataListURL: "/fii/front/school/page",
      getDataListIsPage: true,
      deleteURL: "/fii/front/school/page",
      isNavCollapse:true,
      activeName:"first",
      deleteIsBatch: true,
      dataForm: {
        like_schoolName: "",
      }
    });
    return { ...listService(state), ...toRefs(state)};
  },

  data() {
    return {
      showDialogFlag: false,
      showFenpeiDialogFlag: false,
      showResetPass: false,
      updateForm: {
        id: null,
        schoolName: ''
      },
      formLabelWidth: '120px',
      courseList: [],
      courseSelectedList: [],
      selCourseId: null,
      resetPassForm: {
        id: null,
        password: '',
        password2: ''
      },

    };
  },



  methods: {
    payHandle(orderId: string) {

    },

    clearForm(){
      this.dataForm.like_schoolName = '';
    },

    showDialog(entity: any){
      this.showDialogFlag = true;
      this.updateForm.id = entity.id;
      this.updateForm.schoolName = entity.schoolName;
    },

    showFenpeiDialog(entity: any){
      this.showFenpeiDialogFlag = true;
      this.updateForm.id = entity.id;
      this.querySchool();
      this.querySelectedSchool();
    },

    /**
     * 修改学校信息
     */
    updateSchool(){
      baseService
          .post("/fii/front/school/addOrUpdate", {id:this.updateForm.id, schoolName: this.updateForm.schoolName})
          .then((res) => {
                if (res.code === 0) {
                  this.showDialogFlag = false;
                }
              }
          );
    },

    /**
     * 查询课程 - 下拉列表
     */
    querySchool(){
      const multipleTable = ref<InstanceType<typeof ElTable>>();
      console.log(multipleTable.value)
      baseService
          .get("/fii/front/fiicourse/queryShoolCourse", {schoolId: this.updateForm.id, page:1, limit:1000})
          .then((res) => {
                if (res.code === 0) {
                  this.courseList = res.data.list;
                }
              }
          );
    },

    /**
     * 查询课程 - 已选择课程
     */
    querySelectedSchool(){
      const multipleTable = ref<InstanceType<typeof ElTable>>();
      console.log(multipleTable.value)
      baseService
          .get("/fii/front/fiicourse/querySelectedShoolCourse", {schoolId: this.updateForm.id, page:1, limit:1000})
          .then((res) => {
                if (res.code === 0) {
                  this.courseSelectedList = res.data.list;
                }
              }
          );
    },

    //选中课程
    addOrDelCourse(type: string, id: any) {
      // true就是选中，0或者false是取消选中
      //let selected = selection.length && selection.indexOf(row) !== -1
      console.log(id)
      baseService
          .post("/fii/front/school/addCourse", {
            courseId: type=='add' ? this.selCourseId : id,
            schoolId: this.updateForm.id,
            type: type
          })
          .then((res) => {
                if (res.code === 0) {
                  this.querySchool();
                  this.querySelectedSchool();
                }
              }
          );
    },

    // 回显你已经选中的项
    getRowKeys(row: any) {
      //console.log(row.hasFenpei)
      //判断条件，根据自己的项目进行添加
      if (row.hasFenpei) {
        //一般是你选择项的id
        return true;
      }
    },

    /**
     * 弹出重置窗口
     */
    showResetPassDialog(id: any){
      this.showResetPass = true;
      this.resetPassForm.id = id;
    },

    /**
     * 重置密码
     */
    resetPass(){
      if(this.resetPassForm.password!=this.resetPassForm.password2){
        ElMessage.error('两次输入的密码不一致');
        return;
      }
      baseService
          .post("/fii/front/account/resetPwd", {id:this.resetPassForm.id, password: this.resetPassForm.password})
          .then((res) => {
                if (res.code === 0) {
                  this.showResetPass = false
                }
              }
          );
    }


  }
});
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" >
.search-module {
  display: flex;
  justify-content: space-between;
  padding: 0px 20px 0px 20px;
  margin-top: 10px;
  .interval {
    padding: 0px 15px;
  }

  .search-left {
    color: #333333;
    text-align: left;
  }
  .right-btn {
    text-align: right;
  }
}
.global-table {
  margin-top: 15px;
  padding: 0px 20px;
}

.popper-menu {
  border: 0px !important;
  .el-menu {
    padding: 0px !important;
    background-color: #008fd1 !important;
    .el-menu-item-group__title {
      padding: 0px;
    }
    .el-menu-item:hover {
      background-color: #ffffff !important;
      color: #00a0e9;
    }

    .el-menu-item.is-active {
      background-color: #ffffff !important;
    }
  }
}
</style>
